package service;

import util.DateUtil;
import util.JdbcPoolUtil;
import util.OpentsUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Auther: wy
 * @Date: 2018/9/26 10:16
 * @Description:
 */
public class OdsDataAlarmService {
//    JdbcPoolUtil jdbcPoolUtil = new JdbcPoolUtil();
    Connection connection = null;
    ResultSet rs = null;
    Statement statement = null;

    // 释放资源
    public void destory() throws Exception {
        if (rs != null)
            rs.close();
        if (statement != null)
            statement.close();
        if (connection != null)
            connection.close();
    }

    // tantu_map_business（会运行mapreduce作业）
    public int tantuMapBusinessCountData() throws Exception {
        int count = 0;
        try {
            JdbcPoolUtil jdbcPoolUtil = new JdbcPoolUtil();
            connection = jdbcPoolUtil.getConnection();
            statement = connection.createStatement();
            rs = statement.executeQuery("SELECT count(1) from tantu_map_business WHERE day = '" + DateUtil.getDay() + "' AND hour = '" + DateUtil.getHour() + "'");
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (statement != null)
                    statement.close();
                if (connection != null)
                    connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return count;
    }


    // tantu_map_performance（会运行mapreduce作业）
    public int tantuMapPerformanceCountData() throws Exception {
        int count = 0;
        try {
            JdbcPoolUtil jdbcPoolUtil = new JdbcPoolUtil();
            connection = jdbcPoolUtil.getConnection();
            statement = connection.createStatement();
            rs = statement.executeQuery("SELECT count(1) from tantu_map_performance WHERE day = '" + DateUtil.getDay() + "' AND hour = '" + DateUtil.getHour() + "'");
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (statement != null)
                    statement.close();
                if (connection != null)
                    connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return count;
    }


    // tantu_map_unknown（会运行mapreduce作业）
    public int tantuMapUnknownCountData() throws Exception {
        int count = 0;
        try {
            JdbcPoolUtil jdbcPoolUtil = new JdbcPoolUtil();
            connection = jdbcPoolUtil.getConnection();
            statement = connection.createStatement();
            rs = statement.executeQuery("SELECT count(1) from tantu_map_unknown WHERE day = '" + DateUtil.getDay() + "' AND hour = '" + DateUtil.getHour() + "'");
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (statement != null)
                    statement.close();
                if (connection != null)
                    connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return count;
    }

    // tantu_map_user_action（会运行mapreduce作业）
    public int tantuMapUserActionCountData() throws Exception {
        int count = 0;
        try {
            JdbcPoolUtil jdbcPoolUtil = new JdbcPoolUtil();
            connection = jdbcPoolUtil.getConnection();
            statement = connection.createStatement();
            rs = statement.executeQuery("SELECT count(1) from tantu_map_user_action WHERE day = '" + DateUtil.getDay() + "' AND hour = '" + DateUtil.getHour() + "'");
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (statement != null)
                    statement.close();
                if (connection != null)
                    connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return count;
    }

    // tantu_map_user_page（会运行mapreduce作业）
    public int tantuMapUserPageCountData() throws Exception {
        int count = 0;
        try {
            JdbcPoolUtil jdbcPoolUtil = new JdbcPoolUtil();
            connection = jdbcPoolUtil.getConnection();
            statement = connection.createStatement();
            rs = statement.executeQuery("SELECT count(1) from tantu_map_user_page WHERE day = '" + DateUtil.getDay() + "' AND hour = '" + DateUtil.getHour() + "'");
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (statement != null)
                    statement.close();
                if (connection != null)
                    connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return count;
    }


    public static void main(String[] args) {
        OdsDataAlarmService odsDataAlarmService = new OdsDataAlarmService();
        try {
            OpentsUtil.opentsPut("tantu_map", odsDataAlarmService.tantuMapBusinessCountData(), "tantu_map_business");
            OpentsUtil.opentsPut("tantu_map", odsDataAlarmService.tantuMapPerformanceCountData(), "tantu_map_performance");
            OpentsUtil.opentsPut("tantu_map", odsDataAlarmService.tantuMapUnknownCountData(), "tantu_map_unknown");
            OpentsUtil.opentsPut("tantu_map", odsDataAlarmService.tantuMapUserActionCountData(), "tantu_map_user_action");
            OpentsUtil.opentsPut("tantu_map", odsDataAlarmService.tantuMapUserPageCountData(), "tantu_map_user_page");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
